/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClientSide;

import java.util.ArrayList;
import java.util.logging.*;
import model.WorkingDirectory;
import controller.ControllerWD;
import helper.*;
import model.Data;

/**
 *
 * @author ImNotPro
 */
public class LastRevisionCheacker extends Thread {

    private ArrayList<WorkingDirectory> ServerWD;
    private ArrayList<WorkingDirectory> ClientWD;
    private final int delay = 30;//in second
    private String username;
    private Boolean isNew;

    @Override
    public void run() {
        while (true) {
            try {
                setServerWD(RequstWDtoServer());
                if (isNew) {
                    isNew = false;
                    CheckWD();
                }
                Thread.sleep(delay * 1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * @return the ServerWD
     */
    public ArrayList<WorkingDirectory> getServerWD() {
        return ServerWD;
    }

    /**
     * @param ServerWD the ServerWD to set
     */
    public void setServerWD(ArrayList<WorkingDirectory> ServerWD) {
        this.ServerWD = ServerWD;
    }

    /**
     * @param ClientWD the ClientWD to set
     */
    public void setClientWD(ArrayList<WorkingDirectory> ClientWD) {
        this.ClientWD = ClientWD;
    }

    /*
     * Request HeadRevision To Server
     */
    private ArrayList<WorkingDirectory> RequstWDtoServer() {
        ArrayList<WorkingDirectory> newWD = new ArrayList<WorkingDirectory>();
        try {
            int lastver = Connect.getLatest(username);
            newWD = Connect.getStructure(username, lastver);
            isNew = true;
        } catch (Exception ex) {
            Logger.getLogger(LastRevisionCheacker.class.getName()).log(Level.SEVERE, null, ex);
        }
        return newWD;
    }

    private boolean CompareWD(ArrayList<WorkingDirectory> WD1, ArrayList<WorkingDirectory> WD2) {
        for (int i = 0; i < WD1.size(); ++i) {
            for (int j = 0; j < WD2.size(); ++j) {
                if (WD1.get(i).getDir().equals(WD2.get(j).getDir())) {
                    if (WD1.get(i).getStructDir().size() != WD2.get(j).getStructDir().size()) {
                        return false;
                    } else {
                        if (CompareStrucDir(WD1.get(i).getStructDir(), WD2.get(j).getStructDir())) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    private boolean CompareStrucDir(ArrayList<Data> D1, ArrayList<Data> D2) {
        for (int i = 0; i < D1.size(); ++i) {
            if (!D1.get(i).getByte().equals(D2.get(i).getByte())) {
                return false;
            }
        }
        return true;
    }

    private void CheckWD() {
        ControllerWD.WDclientMaker(ClientWD);
        if (!CompareWD(ServerWD, ClientWD)) {
            System.out.println("New Update");
            ControllerWD.WDclientWriter(ClientWD, ServerWD);
        }
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
}
